﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 玩具商城管理系统;

namespace 玩具商城管理系统
{
    public class Class1
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        public shopcar Add(string shopcar_clientids, int shopcar_commodityids, int shopcar_nums)
        {
            shopcar shopcars = null;   //购物车表

            commodity commoditys = (from p in db.commodity
                                    where p.commodity_id == shopcar_commodityids
                                    select p).First();
            shopcars = new shopcar();
            shopcars.shopcar_clientid = shopcar_clientids;
            shopcars.shopcar_commodityid = commoditys.commodity_id;
            shopcars.shopcar_commodityname = commoditys.commodity_name;
            shopcars.shopcar_money = commoditys.commodity_money.Value;
            shopcars.shopcar_chengb = commoditys.commodity_chengb.Value;
            shopcars.shopcar_num = shopcar_nums;

            //如果客户当前玩具商品已在购物车，则只需要修改数量即可
            int ExistshopcarCount = (from c in db.shopcar
                                     where c.shopcar_clientid == shopcar_clientids && c.shopcar_commodityid == shopcar_commodityids
                                     select c).Count();
            if (ExistshopcarCount > 0)//修改
            {
                shopcar existshopcar = (from c in db.shopcar
                                        where c.shopcar_clientid == shopcar_clientids && c.shopcar_commodityid == shopcar_commodityids
                                        select c).First();
                existshopcar.shopcar_num += shopcar_nums;
            }
            else
            {
                db.shopcar.InsertOnSubmit(shopcars);
            }
            db.SubmitChanges();
            return shopcars;
        }

        public (decimal, decimal) GetTotalPriceByshopcar_clientid(string shopcar_clientids)//计算总价
        {
            List<shopcar> list = (from c in db.shopcar
                                  where c.shopcar_clientid == shopcar_clientids
                                  select c).ToList();
            return ((decimal)list.Sum(c => (c.shopcar_money * c.shopcar_num)), (decimal)list.Sum(c => (c.shopcar_money - c.shopcar_chengb)*c.shopcar_num));
        }
    }
}



